import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {DocumentBuilder, SwaggerModule} from '@nestjs/swagger';
import {DispatchError} from './common/filters/DispatchError';
import { join } from 'path';
import * as fs from 'fs';

import * as session from 'express-session';
import * as passport from 'passport';
import { stringify } from 'querystring';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(session({
        secret: 'keyboard cat',
        name: 'sess-tutorial',
        resave: false,
        saveUninitialized: false,
    }));
  app.use(passport.initialize());
  app.use(passport.session());
  app.useGlobalFilters(new DispatchError());
  const options = new DocumentBuilder()
        .setTitle('User Session Tu2torial')
        .setDescription('Basic Auth and session management')
        .setVersion('1.0')
        .addTag('nestjs')
        .addBearerAuth('Authorization', 'header')
        .build();

  app.useStaticAssets(join(__dirname, '..', 'public'));

  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);

  fs.writeFile(join(__dirname, '..', 'public/document.json'), JSON.stringify(document), 'utf8', (error) => {
    if (error){
        console.log(error);
        return false;
    }
    console.log('写入成功');
  });

  await app.listen(3000);
}
bootstrap();



class A {
  a() {
    const b = new B()
    b.b(this)
  }
}

class B {
  b(a: A){
    console.log(a)
  }
}


